/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import javafx.stage.Stage;
import javafx.stage.Screen;
import javafx.stage.StageStyle;
import model.Model;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import control.Controller;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.AppletStageExtension;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

/**
 * @author Chris
 */
public class MyStage extends IGUI {
    var layouter: Layouter;
    var listener: Controller;
    var stage: Stage;
    var screenBounds: Screen;
    var model: Model;
    var characterDisplay: CharacterDisplay;
    var menuBar: MenuBar;
    var bgImage = ImageView {
        image: Image {
            url: "{__DIR__}fsk/artwork/WoW_WotLK_art01.jpg"
        }

    }




    public override function setModel(modelP: Model) {
        this.model = modelP;
    }

    public override function setActionListener(listenerP: Controller) {
        this.listener = listenerP;
        menuBar.setActionListener(listener);
    }


    public override function createStage(): Stage {
        // Init Layouter
        layouter = Layouter{};
        layouter.init();

        // Init groups
        characterDisplay = CharacterDisplay {};
        characterDisplay.setModel(model);

        //Init control groups
        menuBar = MenuBar {};
        menuBar.setLayouter(layouter);

        
        //Init Dragable
        var stageDragInitialX:Number;
        var stageDragInitialY:Number;

        var inBrowser = (__PROFILE__ == "browser");
        var draggable = if (__PROFILE__ == "tv") then false else AppletStageExtension.appletDragSupported;

        var dragRect:Rectangle = Rectangle {
            x: 0
            y: 0
            width: layouter.stageSize.width
            height: 15
            fill: Color.DARKGRAY
            onMousePressed:function(e) {
                stageDragInitialX = e.screenX - stage.x;
                stageDragInitialY = e.screenY - stage.y;
            }
             onMouseDragged:function(e) {
                stage.x = e.screenX - stageDragInitialX;
                stage.y = e.screenY - stageDragInitialY;
             }
        };
        var dragTextVisible = bind inBrowser and draggable and dragRect.hover;
        var dragControl:Group = Group {
            content: [
                Text {
                    x: 10
                    y: 0
                    content: "Drag out of Browser"
                    fill: Color.GREEN
                    visible: bind dragTextVisible
                },
                ImageView {
                    x: layouter.stageSize.width - 25
                    y: 5
                    image:Image { url: "{__DIR__}menuIcons/close_rollover.png" }
                    visible: bind not inBrowser
                },
                ImageView {
                    x: layouter.stageSize.width - 25
                    y: 5
                    image:Image { url: "{__DIR__}menuIcons/dragOut_rollover.png" }
                    visible: bind inBrowser and draggable
                },
                Rectangle {
                    x: layouter.stageSize.width - 25
                    y: 5
                    width: 10
                    height: 10
                    fill: Color.TRANSPARENT
                    onMouseClicked:function(e:MouseEvent):Void { stage.close(); }
                }
            ]
        };

        var extension =
        if (__PROFILE__ == "tv")
            then null
            else AppletStageExtension {
                shouldDragStart:function(e):Boolean {
                    return inBrowser and e.primaryButtonDown and dragRect.hover;
                }
                onDragStarted:function() {
                    inBrowser = false;
                }
                onAppletRestored:function() {
                    inBrowser = true;
                }
                useDefaultClose: false
        }
            
        stage = Stage {
                    title: bind model.getText();
                    fullScreen: false
                    resizable: true
                    height: layouter.stageSize.height
                    width: layouter.stageSize.width
                    style: StageStyle.TRANSPARENT;
                    scene: Scene {
                        //fill: Color.BLACK
                        content: [
                            bgImage,
                            characterDisplay.getGroup(),
                            menuBar.getGroup(),
                            dragRect,
                            dragControl
                        ]
                    }
                    extensions: [extension]
        }
        layouter.setStage(stage);

        return stage;

    }

}
